﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.DependencyInjection;
using FanaticRock.Core.DI;
using FanaticRock.Core.Extensions;
using FanaticRock.Core.Interfaces;
using FanaticRock.Core.ServiceInterfaces;

namespace FanaticRock.ViewModels
{
    [INotifyPropertyChanged]
    public partial class TestViewModel : ILogger
    {
        #region dependency

        private IAudioPlayService _audioPlayService;

        #endregion

        #region props

        [ObservableProperty] private double _progress = 0d;

        [ObservableProperty] private double _totalDuration = 0d;

        #endregion

        #region ctors

        public TestViewModel(IAudioPlayService audioPlayService)
        {
            _audioPlayService = audioPlayService;

            _audioPlayService.AudioProgressUpdated += _audioPlayService_AudioProgressUpdated;
            PropertyChanged += TestViewModel_PropertyChanged;
        }

        private void _audioPlayService_AudioProgressUpdated(object? sender, Core.AudioPlay.Events.AudioProgressUpdateEventArgs e)
        {
            var totalDuration = e.TotalDuration.TotalSeconds;
            TotalDuration = totalDuration;

            var progress = e.CurrentPosition.TotalSeconds;
            Progress = progress;
        }

        private void TestViewModel_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == nameof(Progress))
            {
                this.Info(_progress);
            }

        }

        #endregion

        #region overrides



        #endregion

        #region singleton

        private static Lazy<TestViewModel> _instance = new Lazy<TestViewModel>(CreateSingleton);
        public static TestViewModel Instance => _instance.Value;

        private static TestViewModel CreateSingleton()
        {
            var instance = new TestViewModel(Ioc.Default.GetService<IAudioPlayService>());


            return instance;
        }

        #endregion

    }
}
